# -*- coding: utf-8 -*-

from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from lxml import etree
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import Select
import time
from selenium.webdriver.common.action_chains import ActionChains
import csv
import os
import re
import requests
from queue import Queue
from threading import Lock, Thread

requests.packages.urllib3.disable_warnings()  # 忽略HTTPS安全警告

"""

"""

# 初始化
def init_driver(url):
    chrome_options = Options()
    # chrome_options.add_argument('--headless') # 无头模式
    chrome_options.add_argument('--disable-gpu')
    chrome_options.add_experimental_option('excludeSwitches', ['enable-automation']) # 设置开发模式，避免被检测
    # driver = webdriver.Chrome(chrome_options=chrome_options) # 已经设置PATH环境变量
    # 未在PATH环境变量中设置，需要设置驱动可执行路径
    driver = webdriver.Chrome(executable_path='D:\\webdriver\\chromedriver.exe', chrome_options=chrome_options)
    driver.maximize_window()  # 窗口最大化
    driver.get(url)
    # html = driver.page_source
    # print(html)
    return driver


# 获取页面url
def get_url(drive, page):
    # 设置等待

    # WebDriverWait(driver, 50).until(
    #     EC.presence_of_element_located((By.ID, "nextpage2"))).click()

    # driver.find_element_by_xpath('//a[@class="ant-pagination-item-link"]').click()
    WebDriverWait(driver, 50).until(
        EC.presence_of_element_located((By.ID, "gridview")))

    selector = etree.HTML(driver.page_source)
    urlList = []
    lineNum = 0
    for s in selector.xpath('//table[@id="gridview"]/tbody/tr'):
        if lineNum > 0:
            dict_data = dict()
            urlChild = "https://www.anpel.com.cn/" + s.xpath('./td[1]/a/@href')[0]
            # dict_data['商品编号'] = s.xpath('./td[1]/a/@title')[0]
            # dict_data['商品名称'] = "".join(s.xpath('./td[2]//text()')).replace("\n", "").strip()
            # dict_data['CAS'] = "".join(s.xpath('./td[3]//text()')).replace("\n", "").strip()
            dict_data['品牌'] = "".join(s.xpath('./td[4]//text()')).replace("\n", "").strip()
            # dict_data['规格型号'] = "".join(s.xpath('./td[5]//text()')).replace("\n", "").strip()
            # dict_data['标准价'] = "".join(s.xpath('./td[6]//text()')).replace("\n", "").strip()
            # dict_data['库存与货期'] = "".join(s.xpath('./td[8]//text()')).replace("\n", "").strip()

            # 限定品牌
            if dict_data['品牌'].find("ANPEL") == 0 or dict_data['品牌'].find("anpel") == 0:
                # 获取当前窗口句柄
                now_handle = driver.current_window_handle
                js = f'window.open("{urlChild}");'
                driver.execute_script(js)
                time.sleep(2)
                # 获取所有窗口句柄
                all_handles = driver.window_handles
                for handle in all_handles:
                    if handle != now_handle:
                        driver.switch_to.window(handle)
                        # 跳转问题。返回312，重新跳转。拼接新的链接地址
                        if driver.title == "":
                            url_tmp = urlChild.split('/')[-1]
                            driver.get(f"https://www.labsci.com.cn/products_{url_tmp}")
                            time.sleep(1)

                        html_child = etree.HTML(driver.page_source)
                        dict_data = dict()
                        spec_tmp = "".join(html_child.xpath('//span[@id="lblSpec"]/text()'))  # 规格
                        dict_data['规格型号_详细'] = spec_tmp
                        dict_data['商品名称'] = "".join(html_child.xpath('//span[@id="lblProductName"]/text()')).replace("标准品", "").strip()  # 规格
                        dict_data['英文名称'] = "".join(html_child.xpath('//span[@id="lblProductNameEng"]/text()'))  # 规格
                        dict_data['商品编号'] = "".join(html_child.xpath('//span[@id="lblStkNo"]/text()'))  # 规格
                        dict_data['品牌'] = "".join(html_child.xpath('//span[@id="lblBrandName"]/text()'))  # 规格
                        dict_data['CAS号'] = "".join(html_child.xpath('//span[@id="lblCasNo"]/text()')) \
                            .replace("[", "").replace("]", "")  # 规格
                        dict_data['CAS号'] = dict_data['CAS号'] + "\t"
                        dict_data['单位'] = "".join(html_child.xpath('//span[@id="lblUnit"]/text()'))  # 规格
                        dict_data['级别'] = "".join(html_child.xpath('//span[@id="lblStte"]/text()'))  # 规格
                        dict_data['标准价'] = "".join(html_child.xpath('//span[@id="lblPrice1"]/text()'))  # 规格
                        dict_data['库存与货期'] = "".join(html_child.xpath('//span[@id="lblTotalQtyMeo"]/text()'))  # 规格
                        dict_data['储存条件'] = "".join(html_child.xpath('//span[@id="lblStorageCondition"]/text()'))  # 规格

                        spec_list = []
                        if len(spec_tmp) > 0:
                            spec_list = spec_tmp.split('，')

                        num = 0
                        for index, value in enumerate(spec_list):
                            word_tmp = ""
                            # 拆分规格型号中的汉字。于..
                            if index == 0 and value.find("于") >= 0:
                                word_list = re.findall('[\u4e00-\u9fa5]+', value)
                                word_tmp = "".join(word_list)
                                dict_data['规格型号' + str(num)] = value.replace(word_tmp, "")
                                word_tmp = word_tmp.replace("于", "")
                                num += 1
                                dict_data['规格型号' + str(num)] = word_tmp
                            else:
                                word_tmp = value
                                dict_data['规格型号' + str(num)] = word_tmp
                            num += 1
                        # 解析类别。1、2级类别
                        class_str = "".join(html_child.xpath('//font[@class="font3"]//text()')).replace("\n", "").strip()
                        class_list = class_str.split(">")
                        dict_data['class_list'] = class_list

                        saveList = []
                        fileName = ""
                        for k, v in enumerate(class_list):
                            if k > 0:
                                saveList.append(v.strip())
                                fileName += v.strip() + "_"
                        find_type(dict_data, html_child)

                        driver.close()
                    # 返回主窗口
                    driver.switch_to.window(now_handle)



                print(f"=========== {urlChild} =============")
                urlList.append(url)
        lineNum += 1

    return urlList

def close_driver(driver):
    driver.quit()

# 整理数据并保存
def find_type(dict_item, selector):
    """
    检索分类，保存数据
    :param fileName: 保存文件名
    :param dict_item: 数据
    :return:
    """
    savefileName = ""
    fileName = ""
    saveList = []
    for k, v in enumerate(dict_item["class_list"]):
        if k > 0:
            saveList.append(v.strip())
            fileName += v.strip() + "_"
    fileName = fileName.replace("/", "-")

    if dict_item["商品名称"].find("混标") > 0:
        savefileName = fileName + "混标.csv"
        # 商品信息 保存
        hb(selector, saveList, dict_item, savefileName)
    elif (dict_item["规格型号_详细"].find("/L") >= 0) or (dict_item["规格型号_详细"].find("/l") >= 0) \
            or (dict_item["规格型号_详细"].find("/ML") >= 0) or (dict_item["规格型号_详细"].find("/mL") >= 0) \
            or (dict_item["规格型号_详细"].find("/ul") >= 0) or (dict_item["规格型号_详细"].find("/ml") >= 0) \
            or (dict_item["规格型号_详细"].find("/ampo") >= 0) or (dict_item["规格型号_详细"].find("ml") >= 0) \
            or (dict_item["规格型号_详细"].find("mL") >= 0) or (dict_item["规格型号_详细"].find("ML") >= 0) \
            or (dict_item["规格型号_详细"].find("umol") >= 0) or (dict_item["规格型号_详细"].find("L") >= 0) \
            or (dict_item["规格型号_详细"].find("ul") >= 0):
        savefileName = fileName + "液标.csv"
    elif (dict_item["规格型号_详细"].find("/mg") >= 0) or (dict_item["规格型号_详细"].find("/g") >= 0) \
            or (dict_item["规格型号_详细"].find("mg") >= 0) or (dict_item["规格型号_详细"].find("g") >= 0) \
            or (dict_item["规格型号_详细"].find("纯品") >= 0) or (dict_item["规格型号_详细"].find("MG") >= 0) \
            or (dict_item["规格型号_详细"].find("G") >= 0) or (dict_item["规格型号_详细"].find("gm") >= 0):
        savefileName = fileName + "_固标.csv"
    else:
        if (dict_item["规格型号_详细"].find("/L") >= 0) or (dict_item["规格型号_详细"].find("/l") >= 0) \
                or (dict_item["规格型号_详细"].find("/ML") >= 0) or (dict_item["规格型号_详细"].find("/mL") >= 0) \
                or (dict_item["规格型号_详细"].find("/ul") >= 0) or (dict_item["规格型号_详细"].find("/ml") >= 0) \
                or (dict_item["规格型号_详细"].find("/ampo") >= 0) or (dict_item["规格型号_详细"].find("ml") >= 0) \
                or (dict_item["规格型号_详细"].find("mL") >= 0):
            savefileName = fileName + "液标.csv"
        else:
            savefileName = fileName + "其他.csv"
    try:
        # 移出不需要保存的键值
        dict_item.pop('class_list')
    except:
        pass

    for k, v in dict_item.items():
        saveList.append(v)

    if os.path.exists(savefileName) == False:
        with open(savefileName, "a", encoding="utf-8-sig", newline="") as csvfile:
            writer = csv.writer(csvfile)
            writer.writerow(["大类", "子类", "规格型号_详细", "商品名称", "英文名称", "商品编号", "品牌",
                             "CAS号", "单位", "级别", "标准价", "库存与货期", "储存条件", "规格型号_1", "规格型号_2", "规格型号_3"])
    if dict_item["商品名称"].find("混标") < 0:
        with open(savefileName, "a", encoding="utf-8-sig", newline="") as csvfile:
            writer = csv.writer(csvfile)
            writer.writerow(saveList)

# 混标-商品信息
def hb(selector, class_list, dict_data, savefileName):
    # 移出不需要保存的键值
    dict_data.pop('class_list')
    if len(selector.xpath('//div[@class="informationDiv"]/table/tbody/tr')) > 0:
        flag = False
        # 商品信息
        for d in selector.xpath('//div[@class="informationDiv"]/table/tbody/tr'):
            tmp_list = []
            tmp_list = class_list.copy()
            txt = "".join(d.xpath('.//text()')).replace('\n', '').strip()
            if txt not in ('', '~H', '~He ', '~He', '~Be', '~B', '~N', '~O', '~F', '~K', '~P'):
                flag = True
                info_tmp = txt
                # dict_data['信息'] = info_tmp
                if len(info_tmp.split('|')) > 1:
                    source_list = info_tmp.split('|')
                else:
                    source_list = info_tmp.split()

                for index, value in enumerate(source_list):
                    dict_data['商品信息' + str(index)] = value

                for key, value in dict_data.items():
                    tmp_list.append(value)

                try:
                    for n in range(0, index + 1):
                        dict_data.pop('商品信息' + str(n))
                except:
                    pass

                with open(savefileName, "a", encoding="utf-8-sig", newline="") as csvfile:
                    writer = csv.writer(csvfile)
                    writer.writerow(tmp_list)
    elif len(selector.xpath('//div[@class="informationDiv"]/div[@style="“background-color:"]/div')) > 0:
        flag = False
        # 商品信息
        for d in selector.xpath('//div[@class="informationDiv"]/div[@style="“background-color:"]/div'):
            tmp_list = []
            tmp_list = class_list.copy()
            txt = "".join(d.xpath('.//text()')).replace('\n', '').replace('\t', '').strip()
            if txt not in ('', '~H', '~He ', '~He', '~Be', '~B', '~N', '~O', '~F', '~K', '~P'):
                info_tmp = txt
                # dict_data['信息'] = info_tmp
                if len(info_tmp.split('|')) > 1:
                    source_list = info_tmp.split('|')
                else:
                    source_list = info_tmp.split()

                for index, value in enumerate(source_list):
                    dict_data['商品信息' + str(index)] = value

                for key, value in dict_data.items():
                    tmp_list.append(value)

                try:
                    for n in range(0, index + 1):
                        dict_data.pop('商品信息' + str(n))
                except:
                    pass

                with open(savefileName, "a", encoding="utf-8-sig", newline="") as csvfile:
                    writer = csv.writer(csvfile)
                    writer.writerow(tmp_list)
        if flag == False:
            for key, value in dict_data.items():
                tmp_list.append(value)

            with open(savefileName, "a", encoding="utf-8-sig", newline="") as csvfile:
                writer = csv.writer(csvfile)
                writer.writerow(tmp_list)
    elif len(selector.xpath('//div[@class="informationDiv"]/div[@style="background-color: rgb(245, 245, 245);"]/div')) > 0:
        flag = False
        # 商品信息
        for d in selector.xpath('//div[@class="informationDiv"]/div[@style="background-color: rgb(245, 245, 245);"]/div'):
            tmp_list = []
            tmp_list = class_list.copy()
            txt = "".join(d.xpath('.//text()')).replace('\n', '').replace('\t', '').strip()
            if txt not in ('', '~H', '~He ', '~He', '~Be', '~B', '~N', '~O', '~F', '~K', '~P'):
                info_tmp = txt
                # dict_data['信息'] = info_tmp
                if len(info_tmp.split('|')) > 1:
                    source_list = info_tmp.split('|')
                else:
                    source_list = info_tmp.split()

                for index, value in enumerate(source_list):
                    dict_data['商品信息' + str(index)] = value

                for key, value in dict_data.items():
                    tmp_list.append(value)

                try:
                    for n in range(0, index + 1):
                        dict_data.pop('商品信息' + str(n))
                except:
                    pass

                with open(savefileName, "a", encoding="utf-8-sig", newline="") as csvfile:
                    writer = csv.writer(csvfile)
                    writer.writerow(tmp_list)
        if flag == False:
            for key, value in dict_data.items():
                tmp_list.append(value)

            with open(savefileName, "a", encoding="utf-8-sig", newline="") as csvfile:
                writer = csv.writer(csvfile)
                writer.writerow(tmp_list)
    elif len(selector.xpath('//div[@class="informationDiv"]/div/div/div/text()')) > 0:
        flag = False
        # 商品信息
        # for d in selector.xpath('//div[@class="informationDiv"]//text()'):
        for d in selector.xpath('//div[@class="informationDiv"]/div/div/div/text()'):
            tmp_list = []
            tmp_list = class_list.copy()
            # txt = d.replace('\n', '').strip()
            txt = d.replace('\n', '').replace('\t', ' ').strip()
            if txt not in ('', '~H', '~He ', '~He', '~Be', '~B', '~N', '~O', '~F', '~K', '~P'):
                flag = True
                info_tmp = txt
                # dict_data['信息'] = info_tmp
                if len(info_tmp.split('|')) > 1:
                    source_list = info_tmp.split('|')
                else:
                    source_list = info_tmp.split()

                for index, value in enumerate(source_list):
                    dict_data['商品信息' + str(index)] = value

                for key, value in dict_data.items():
                    tmp_list.append(value)

                try:
                    for n in range(0, index + 1):
                        dict_data.pop('商品信息' + str(n))
                except:
                    pass

                with open(savefileName, "a", encoding="utf-8-sig", newline="") as csvfile:
                    writer = csv.writer(csvfile)
                    writer.writerow(tmp_list)
        if flag == False:
            for key, value in dict_data.items():
                tmp_list.append(value)

            with open(savefileName, "a", encoding="utf-8-sig", newline="") as csvfile:
                writer = csv.writer(csvfile)
                writer.writerow(tmp_list)
    elif len(selector.xpath('//div[@class="informationDiv"]/div/br')) > 0:
        flag = False
        # 商品信息
        for d in selector.xpath('//div[@class="informationDiv"]//text()'):
            # for d in selector.xpath('//div[@class="informationDiv"]/div/text()'):
            tmp_list = []
            tmp_list = class_list.copy()

            # txt = d.replace('\n', '').strip()
            txt = d.replace('\n', '').replace('\t', ' ').strip()
            if txt not in ('', '~H', '~He ', '~He', '~Be', '~B', '~N', '~O', '~F', '~K', '~P'):
                flag = True
                info_tmp = txt
                # dict_data['信息'] = info_tmp
                if len(info_tmp.split('|')) > 1:
                    source_list = info_tmp.split('|')
                else:
                    source_list = info_tmp.split()

                for index, value in enumerate(source_list):
                    dict_data['商品信息' + str(index)] = value

                for key, value in dict_data.items():
                    tmp_list.append(value)

                try:
                    for n in range(0, index + 1):
                        dict_data.pop('商品信息' + str(n))
                except:
                    pass

                with open(savefileName, "a", encoding="utf-8-sig", newline="") as csvfile:
                    writer = csv.writer(csvfile)
                    writer.writerow(tmp_list)
        if flag == False:
            for key, value in dict_data.items():
                tmp_list.append(value)

            with open(savefileName, "a", encoding="utf-8-sig", newline="") as csvfile:
                writer = csv.writer(csvfile)
                writer.writerow(tmp_list)
    elif len(selector.xpath('//div[@class="informationDiv"]/div/p')) > 0:
        flag = False
        # 商品信息
        # for d in selector.xpath('//div[@class="informationDiv"]//text()'):
        for d in selector.xpath('//div[@class="informationDiv"]/div/p'):
            tmp_list = []
            tmp_list = class_list.copy()
            # txt = d.replace('\n', '').strip()
            txt = "".join(d.xpath('.//text()')).replace('\n', '').replace('\t', ' ').strip()
            if txt not in ('', '~H', '~He ', '~He', '~Be', '~B', '~N', '~O', '~F', '~K', '~P'):
                flag = True
                info_tmp = txt
                # dict_data['信息'] = info_tmp
                if len(info_tmp.split('|')) > 1:
                    source_list = info_tmp.split('|')
                else:
                    source_list = info_tmp.split()

                for index, value in enumerate(source_list):
                    dict_data['商品信息' + str(index)] = value

                for key, value in dict_data.items():
                    tmp_list.append(value)

                try:
                    for n in range(0, index + 1):
                        dict_data.pop('商品信息' + str(n))
                except:
                    pass

                with open(savefileName, "a", encoding="utf-8-sig", newline="") as csvfile:
                    writer = csv.writer(csvfile)
                    writer.writerow(tmp_list)
        if flag == False:
            for key, value in dict_data.items():
                tmp_list.append(value)

            with open(savefileName, "a", encoding="utf-8-sig", newline="") as csvfile:
                writer = csv.writer(csvfile)
                writer.writerow(tmp_list)
        # elif len(selector.xpath('//div[@class="informationDiv"]//text()')) > 0:
    elif len(selector.xpath('//div[@class="informationDiv"]/div')) > 0:
        flag = False
        # 商品信息
        # for d in selector.xpath('//div[@class="informationDiv"]//text()'):
        for d in selector.xpath('//div[@class="informationDiv"]/div'):
            tmp_list = []
            tmp_list = class_list.copy()
            # txt = d.replace('\n', '').strip()
            txt = "".join(d.xpath('.//text()')).replace('\n', '').replace('\t', ' ').strip()
            if txt not in ('', '~H', '~He ', '~He', '~Be', '~B', '~N', '~O', '~F', '~K', '~P'):
                flag = True
                info_tmp = txt
                # dict_data['信息'] = info_tmp
                if len(info_tmp.split('|')) > 1:
                    source_list = info_tmp.split('|')
                else:
                    source_list = info_tmp.split()

                for index, value in enumerate(source_list):
                    dict_data['商品信息' + str(index)] = value

                for key, value in dict_data.items():
                    tmp_list.append(value)

                try:
                    for n in range(0, index + 1):
                        dict_data.pop('商品信息' + str(n))
                except:
                    pass

                with open(savefileName, "a", encoding="utf-8-sig", newline="") as csvfile:
                    writer = csv.writer(csvfile)
                    writer.writerow(tmp_list)
        if flag == False:
            for key, value in dict_data.items():
                tmp_list.append(value)

            with open(savefileName, "a", encoding="utf-8-sig", newline="") as csvfile:
                writer = csv.writer(csvfile)
                writer.writerow(tmp_list)
    elif len(selector.xpath('//div[@class="informationDiv"]//text()')) > 0:
        flag = False
        # 商品信息
        # for d in selector.xpath('//div[@class="informationDiv"]//text()'):
        for d in selector.xpath('//div[@class="informationDiv"]//text()'):
            tmp_list = []
            tmp_list = class_list.copy()
            # txt = d.replace('\n', '').strip()
            txt = d.replace('\n', '').replace('\t', ' ').strip()
            if txt not in ('', '~H', '~He ', '~He', '~Be', '~B', '~N', '~O', '~F', '~K', '~P'):
                flag = True
                info_tmp = txt
                # dict_data['信息'] = info_tmp
                if len(info_tmp.split('|')) > 1:
                    source_list = info_tmp.split('|')
                else:
                    source_list = info_tmp.split()

                for index, value in enumerate(source_list):
                    dict_data['商品信息' + str(index)] = value

                for key, value in dict_data.items():
                    tmp_list.append(value)

                try:
                    for n in range(0, index + 1):
                        dict_data.pop('商品信息' + str(n))
                except:
                    pass

                with open(savefileName, "a", encoding="utf-8-sig", newline="") as csvfile:
                    writer = csv.writer(csvfile)
                    writer.writerow(tmp_list)
        if flag == False:
            for key, value in dict_data.items():
                tmp_list.append(value)

            with open(savefileName, "a", encoding="utf-8-sig", newline="") as csvfile:
                writer = csv.writer(csvfile)
                writer.writerow(tmp_list)
    else:
        print("")


if __name__ == '__main__':
    url = "https://www.labsci.com.cn/category_930.html"
    driver = init_driver(url)

    pagenum = 1
    while True:
        get_url(driver, pagenum)

        parm = f'//table[@id="dtListPag"]/tbody/tr/td/a[contains(@style,"background-color:#0088FF;")]/text()'
        root = etree.HTML(driver.page_source)
        currentpageNum = int(root.xpath('//input[@id="txtToPageNum"]/@value')[0])  # 文本框数字

        currentNum = int(root.xpath(parm)[0])  # 当前页号
        if currentNum < currentpageNum:
            pagenum = currentNum + 1
            parm_next = f'//table[@id="dtListPag"]/tbody/tr/td/a[contains(text(),"{pagenum}")]'
            driver.find_element_by_xpath(parm_next).click()
            time.sleep(2)
            print(f"============第【{pagenum}】页=============")
        else:
            break
    driver.quit()



